package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.CvdProductEntity;
import com.hitqz.robot.api.business.dto.CvdProductQueryDTO;
import com.hitqz.robot.api.business.dto.CvdProductSaveDTO;
import com.hitqz.robot.api.business.dto.CvdProductUpdateDTO;
import com.hitqz.robot.api.business.vo.CvdProductVO;
import com.hitqz.robot.biz.mapper.CvdProductMapper;
import com.hitqz.robot.biz.service.CvdProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * CVD产品管理
 *
 * @author kehanjiang
 * @date 2025-09-19 10:48:23
 */
@Service
@RequiredArgsConstructor
public class CvdProductServiceImpl extends ServiceImpl<CvdProductMapper, CvdProductEntity> implements CvdProductService {

	private CvdProductVO convertToVO(Object o) {
        if (o instanceof CvdProductEntity) {
            CvdProductVO vo = new CvdProductVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
			
            return vo;
        }
        return null;
    }
	
	private void preCheck(CvdProductEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
        } else {
           //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CvdProductVO> getCvdProductPage(Page page, CvdProductQueryDTO dto){
        CvdProductEntity entity = new CvdProductEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        entity.setCode(null);
        final String name = dto.getName();
        final String code = dto.getCode();
        LambdaQueryWrapper<CvdProductEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), CvdProductEntity::getName, name);
        wrapper.like(StringUtils.hasText(code), CvdProductEntity::getCode, code);
        wrapper.orderByDesc(CvdProductEntity::getId);
        return (Page<CvdProductVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public CvdProductVO getCvdProductById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveCvdProduct(CvdProductSaveDTO dto){
        CvdProductEntity entity = new CvdProductEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateCvdProductById(CvdProductUpdateDTO dto){
        CvdProductEntity entity = new CvdProductEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeCvdProductBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}